package framework.jackson;

import com.fasterxml.jackson.annotation.JsonRawValue;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringEscapeUtils;
import org.junit.Test;
import spring.validation.Person;

import java.util.Map;
import java.util.StringJoiner;

/**
 * 反序列化成对象时，空字符串会设置到属性，而不存在的属性是空。
 *
 * @author ME
 * @date   2019年12月13日 下午4:48:19
 */
public class TestJacksonAPI {

    /**
     * 测试 空对象 和 null
     */
    @Test
    public void testEmptyAndNon() throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        String str = "{\"name\":\"lj-y\",\"age\":\"10\",\"testSize\":\"\",\"testLength\":\"9\"}";
        Person person = objectMapper.readValue(str, Person.class);
        // Person{name='ljy', age='', testSize='', hobby=null}
        System.out.println(person);
        System.out.println("testSize.length(): " + person.getTestSize().length());
        System.out.println("testLength.length(): " + person.getTestLength().length());

        Map<String, Object> map = objectMapper.readValue(str, new TypeReference<Map<String, Object>>(){});
        System.out.println(map);
    }

    /**
     * 序列化、反序列化时，@JsonRawValue 定义的值表示使用原本的 字面量
     */
    @Test
    public void test() throws Exception {
        String str = "{" +
                "        \"businessKey\":\"SJTC202302010001\",\n" +
                "        \"taskName\":\"公共事务部\\n直属主管\",\n" +
                "        \"event\":\"ActivityStarted\"}";
        ObjectMapper mapper = new ObjectMapper();
        CallBackVo callBackVo = mapper.readValue(str, CallBackVo.class);
        System.out.println(callBackVo);

        String str3 = mapper.writeValueAsString(callBackVo);
        callBackVo = mapper.readValue(str3, CallBackVo.class);
        System.out.println(callBackVo);

        // 等效于 json 反转义
        String str2 = StringEscapeUtils.unescapeJson(str);
        str2 = "{" +
                "        \"businessKey\":\"SJTC202302010001\",\n" +
                "        \"taskName\":\"公共事务部\n直属主管\",\n" +
                "        \"event\":\"ActivityStarted\"}";
        callBackVo = mapper.readValue(str2, CallBackVo.class);
        System.out.println(callBackVo);
    }

    private static class CallBackVo {
        String businessKey;
        // @JsonRawValue
        String taskName;
        String event;

        public String getBusinessKey() {
            return businessKey;
        }

        public void setBusinessKey(String businessKey) {
            this.businessKey = businessKey;
        }

        public String getTaskName() {
            return taskName;
        }

        public void setTaskName(String taskName) {
            this.taskName = taskName;
        }

        public String getEvent() {
            return event;
        }

        public void setEvent(String event) {
            this.event = event;
        }

        @Override
        public String toString() {
            return new StringJoiner(", ", CallBackVo.class.getSimpleName() + "[", "]")
                    .add("businessKey='" + businessKey + "'")
                    .add("taskName='" + taskName + "'")
                    .add("event='" + event + "'")
                    .toString();
        }
    }

}
